package com.party.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.party.entity.User;
import com.party.service.IUserService;
import com.party.service.impl.UserServiceImpl;
import com.party.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者
 * @since 2024-06-10
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    Result result = new Result();

    @PostMapping("/register")
    public Result register(User user) {
        User foundOne = userService.getOne(new QueryWrapper<User>()
                .eq("account", user.getAccount()));
        if (foundOne != null) {
//            已存在
            result.setCode(403);
            return result;
        }
        userService.save(user);
        result.setCode(200);
        return result;
    }



    @PostMapping
    public Result login(User user) {
        User foundOne = userService.getOne(new QueryWrapper<User>()
                .eq("account", user.getAccount()));
        if (foundOne != null) {
//            密码
            if (foundOne.getPassword().equals(user.getPassword())) {
//                通过
                result.setCode(200);
                return result;
            }
            result.setCode(403);
            return result;
        }
        result.setCode(404);
        return result;
    }

}
